package com.whz.workmanageol.controller;

import com.whz.workmanageol.service.ExcelReadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 完善Excel信息 并将数据写入数据库
 */
@Controller
@RequestMapping(value = "/admin/readexcel")
public class ExcelReadController {
    @Autowired
    private ExcelReadService excelReadService;

    private static final Logger logger = LoggerFactory.getLogger(ExcelReadController.class);

    @PostMapping("studentInfo")
    public String studentInfo(@RequestParam("file") MultipartFile file,
                              HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"studentinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的studentinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.studentInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去学生信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("teacherInfo")
    public String teacherInfo(@RequestParam("file") MultipartFile file,
                              HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"teacherinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的teacherinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.teacherInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去教师信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("gradeInfo")
    public String gradeInfo(@RequestParam("file") MultipartFile file,
                              HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"gradeinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的gradeinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.gradeInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去班级信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("courseInfo")
    public String courseInfo(@RequestParam("file") MultipartFile file,
                            HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"courseinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的courseinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.courseInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去课程信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("gradeCourseInfo")
    public String gradeCourseInfo(@RequestParam("file") MultipartFile file,
                             HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"gradecourseinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的gradecourseinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.gradeCourseInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去班级课程信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("stuGradeInfo")
    public String stuGradeInfo(@RequestParam("file") MultipartFile file,
                                  HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"studentgradeinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的studentgradeinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.studentGradeInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去学生班级信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("stuCourseInfo")
    public String stuCourseInfo(@RequestParam("file") MultipartFile file,
                               HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"studentcourseinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的studentcourseinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.studentCourseInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去学生课程信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("teaGradeInfo")
    public String teaGradeInfo(@RequestParam("file") MultipartFile file,
                                HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"teachergradeinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的teachergradeinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.teaGradeInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去教师班级信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }

    @PostMapping("teaCourseInfo")
    public String teaCourseInfo(@RequestParam("file") MultipartFile file,
                               HttpServletResponse response){
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if("".equals(file.getOriginalFilename()) || !"teachercourseinfo.xlsx".equals(file.getOriginalFilename())){
            String info = "<script language=\"javascript\">alert('请选中完善后的teachercourseinfo.xlsx');</script>";
            out.print(info);
            return "bootstrap/importinfo";

        }
        excelReadService.teaCourseInfo(file);
        String info = "<script language=\"javascript\">alert('正在上传数据 请去教师课程信息页面核实数据数量');</script>";
        out.print(info);
        return "bootstrap/importinfo";
    }


}
